package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.mapper.ApReadBehaviorMapper;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApReadBehaviorService;
import com.heima.common.constants.article.HotArticleConstants;
import com.heima.model.behavior.dtos.ReadBehaviorDto;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApReadBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.app.UpdateArticleMess;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.UUID;

/**
 * @作者 itcast
 * @创建日期 2020/10/8 14:35
 **/
@Service
@Transactional
public class ApReadBehaviorServiceImpl extends ServiceImpl<ApReadBehaviorMapper, ApReadBehavior> implements ApReadBehaviorService {
    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;
    @Override
    public ResponseResult readBehavior(ReadBehaviorDto dto) {
        // 1. 检查参数
        if(dto == null || dto.getArticleId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2. 查询行为实体
        ApUser user = AppThreadLocalUtils.getUser();
        ApBehaviorEntry behaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());
        if(behaviorEntry == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 3. 查询是否记录过阅读行为
        ApReadBehavior readBehavior = getOne(
                Wrappers.<ApReadBehavior>lambdaQuery()
                        .eq(ApReadBehavior::getArticleId, dto.getArticleId())
                        .eq(ApReadBehavior::getEntryId, behaviorEntry.getId()));
        if(readBehavior == null){
            // 3.1 没有记录 保存新记录
            readBehavior = new ApReadBehavior();
            readBehavior.setArticleId(dto.getArticleId());
            readBehavior.setCount(dto.getCount());
            readBehavior.setEntryId(behaviorEntry.getId());
            readBehavior.setLoadDuration(dto.getLoadDuration());
            readBehavior.setReadDuration(dto.getReadDuration());
            readBehavior.setPercentage(dto.getPercentage());
            readBehavior.setCreatedTime(new Date());
            // 保存阅读行为
            save(readBehavior);
        }else {
            // 3.2 有记录 阅读次数+1 更新修改时间
            readBehavior.setUpdatedTime(new Date());
            readBehavior.setCount((short) (readBehavior.getCount() + 1));
            readBehavior.setReadDuration(readBehavior.getReadDuration() + dto.getReadDuration());
            updateById(readBehavior);
        }
        // 发送消息
            UpdateArticleMess mess = new UpdateArticleMess();
            mess.setAdd(1);
            mess.setType(UpdateArticleMess.UpdateArticleType.VIEWS);
            mess.setArticleId(dto.getArticleId());
            kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC, UUID.randomUUID().toString(), JSON.toJSONString(mess));
        return ResponseResult.okResult();
    }
    @Autowired
    KafkaTemplate<String,String> kafkaTemplate;
}
